package com.hxx.widget.richtext

import android.graphics.Typeface
import android.text.Layout
import android.text.style.*
import com.hxx.widget.richtext.views.NumberBulletSpan

/**
 * 文本样式创建
 */
class SpanFactory {
    companion object {
        fun absoluteSizeSpan(size: Int): AbsoluteSizeSpan = AbsoluteSizeSpan(size, true)
        fun alignmentSpan(alignment: Layout.Alignment): AlignmentSpan =
            AlignmentSpan.Standard(alignment)
        fun backgroundColorSpan(color: Int): BackgroundColorSpan = BackgroundColorSpan(color)
        fun foregroundColorSpan(color: Int): ForegroundColorSpan = ForegroundColorSpan(color)
        fun quoteSpan(color: Int): QuoteSpan = QuoteSpan(color)
        fun styleSpan(style: Int = Typeface.BOLD): StyleSpan = StyleSpan(style)
        fun relativeSizeSpan(proportion: Float): RelativeSizeSpan = RelativeSizeSpan(proportion)
        fun strikethroughSpan() = StrikethroughSpan()
        fun underlineSpan() = UnderlineSpan()

        /**
         * 复制相同属性的span
         */
        fun copySpan(span: Any): Any? {
            return when (span) {
                is AbsoluteSizeSpan -> AbsoluteSizeSpan(span.size, span.dip)
                is AlignmentSpan -> AlignmentSpan.Standard(span.alignment)
                is NumberBulletSpan -> NumberBulletSpan(1)
                is BulletSpan -> BulletSpan(50)
                is BackgroundColorSpan -> BackgroundColorSpan(span.backgroundColor)
                is ForegroundColorSpan -> ForegroundColorSpan(span.foregroundColor)
                is QuoteSpan -> QuoteSpan(span.color)
                is StyleSpan -> StyleSpan(span.style)
                is RelativeSizeSpan -> RelativeSizeSpan(span.sizeChange)
                is StrikethroughSpan -> StrikethroughSpan()
                is UnderlineSpan -> UnderlineSpan()
                else -> null
            }
        }
    }
}